package com.palfund.service;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

//stop MainActivity start的service
public class TestActivity extends AppCompatActivity {

    private ServiceConnection mServiceConnection;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_bind);
        Intent intent = new Intent(this, BindService.class);
        mServiceConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                Log.i("TestActivity", "---onServiceConnected()--->" + "TestBind绑定成功");
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        };
          bindService(intent, mServiceConnection, BIND_AUTO_CREATE);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(mServiceConnection);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //stop MainActivity start的service
        menu.add(Menu.NONE, 1, 1, "stop start service");
        return super.onCreateOptionsMenu(menu);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case 1:
                stopService(new Intent(this, StartService.class));
                break;


        }
        return super.onOptionsItemSelected(item);
    }
}
